package com.example.springboot.controller;

import com.example.springboot.common.MD5Util;
import com.example.springboot.common.Result;
import com.example.springboot.controller.DTO.PassowrdUpdataDTO;
import com.example.springboot.controller.DTO.UserQueryDTO;
import com.example.springboot.dao.mUserDao;
import com.example.springboot.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class mUserController {
    @Autowired
    private mUserDao muserDao;

    @PostMapping("/login")
    public Result Login(@RequestBody User user){
        if(user.getUsername()==null||user.getPassword()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        User res = muserDao.getUser(user.getUsername());
        String pw = MD5Util.MD5Encode(user.getPassword(), "UTF-8");
        User res =  muserDao.getByUser(user.getUsername(), pw);
        if(res==null){
            //throw new RuntimeException("用户名密码错误");
            return Result.error("用户名密码错误");
        }
            return Result.success(res);

    }

    @GetMapping
    public Result findAll(){
        return Result.success(muserDao.findAll());
    }

    @GetMapping("/page")// /user/page?currentPage= & pageSize=
    public Result findPage(UserQueryDTO userQueryDTO){

        if(userQueryDTO.getCurrentPage()==null||userQueryDTO.getCurrentPage()<=0||userQueryDTO.getPageSize()<1){
            return Result.error("参数错误");
        }
        Integer currentPage = userQueryDTO.getCurrentPage();
        Integer pageSize = userQueryDTO.getPageSize();
//        Integer pageNum = (currentPage-1)*pageSize;
//        userQueryDTO.setPageNum(pageNum);
//        long total = muserDao.count(userQueryDTO);

//        Map<String, Object> map = new HashMap<>();
//        map.put("data", users);
//        map.put("total", total);
        PageHelper.startPage(currentPage, pageSize);
        List<User> users = muserDao.findByOptions(userQueryDTO);
        return Result.success(new PageInfo<>(users));
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(muserDao.getById(id));
    }

    @GetMapping("/UorP")
    public Result getByUser(String username, String password){
        String pw = MD5Util.MD5Encode(password, "UTF-8");
        User user = muserDao.getByUser(username, pw);
        if(user==null) return Result.error("密码或用户名错误");
        return Result.success(muserDao.getByUser(username, pw));
    }

    @PostMapping
    public Result Save(@RequestBody User user){
        if(user.getUsername()==null||user.getPassword()==null){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        String pw = MD5Util.MD5Encode(user.getPassword(), "UTF-8");
        user.setPassword(pw);
        return Result.success(muserDao.insert(user));
    }

    @PutMapping
    public Result Updata(@RequestBody User user){
        if(user.getId()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
        return Result.success(muserDao.updata(user));
    }

   @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
        if(id==null||id==0){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(muserDao.deleteById(id)==1);
    }
    @PostMapping("/password")
    public Result UpdataPassword(@RequestBody PassowrdUpdataDTO passowrdUpdataDTO){
        Integer id = passowrdUpdataDTO.getId();
        String password = passowrdUpdataDTO.getPassword();
        String newPassword = passowrdUpdataDTO.getNewPassword();
        //System.out.println(id+" 222 "+newPassword);
        String pw = MD5Util.MD5Encode(newPassword, "UTF-8");
        if(id==null||password==null){
            return Result.error("参数错误");
        }
        User user = muserDao.getById(id);
        if(user == null){
            return Result.error("用户不存在");
        }else{
            return Result.success(muserDao.updataPassword(id, pw));
        }
    }

}
